package es.deusto.tralala.authentication;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;

public class AuthentificationServer extends UnicastRemoteObject implements IAuthentificationServer {

	private static final long serialVersionUID = 1L;
	private Map<String, String> users = new HashMap<>();

	public AuthentificationServer() throws RemoteException {
		users.put("user", "user");
	}
	public static void main(String[] args) {
		if (args.length != 3) {
			System.out.println("usage: java [policy] [codebase] server.Server [host] [port] [server]");
			System.exit(0);
		}

		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}

		String name = "//" + args[0] + ":" + args[1] + "/" + args[2];

		try {
			IAuthentificationServer objServidor = new AuthentificationServer();
			Naming.rebind(name, objServidor);
			System.out.println("* Server '" + name + "' active and waiting...");
			
		} catch (Exception e) {
			System.err.println("- Exception running the server: " + e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	public boolean login(String user, String pass){
		if (users.containsKey(user)) {
			if (users.get(user).equals(pass)) {
				return true;
			}
		}
		
		return false;
	}
}
